home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 17 / RECYCLE2.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  4KB  |  144 lines

  1. // File from page 738 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: RECYCLE2.CPP -- Chapter 14 example w/ RTTI
  34. #include <fstream.h>
  35. #include <stdlib.h>
  36. #include <time.h>
  37. #include <typeinfo.h>
  38. #include "..\14\tstack.h"
  39. ofstream out("recycle2.out");
  40.  
  41. class trash {
  42.   float Weight;
  43. public:
  44.   trash(float Wt) : Weight(Wt) {}
  45.   virtual float value() const = 0;
  46.   float weight() const { return Weight; }
  47.   virtual ~trash() {}
  48. };
  49.  
  50. class aluminum : public trash {
  51.   static float val;
  52. public:
  53.   aluminum(float Wt) : trash(Wt) {}
  54.   float value() const { return val; }
  55.   static void value(int newval) {
  56.     val = newval;
  57.   }
  58. };
  59.  
  60. float aluminum::val = 1.67;
  61.  
  62. class paper : public trash {
  63.   static float val;
  64. public:
  65.   paper(float Wt) : trash(Wt) {}
  66.   float value() const { return val; }
  67.   static void value(int newval) {
  68.     val = newval;
  69.   }
  70. };
  71.  
  72. float paper::val = 0.10;
  73.  
  74. class glass : public trash {
  75.   static float val;
  76. public:
  77.   glass(float Wt) : trash(Wt) {}
  78.   float value() const { return val; }
  79.   static void value(int newval) {
  80.     val = newval;
  81.   }
  82. };
  83.  
  84. float glass::val = 0.23;
  85.  
  86. // Sums up the value of the trash in a bin:
  87. template<class T> void
  88. SumValue(const tstack<T>& bin, ostream& os) {
  89.   tstackIterator<T> tally(bin);
  90.   float val = 0;
  91.   while(tally) {
  92.     val += tally->weight() * tally->value();
  93.     os << "weight of "
  94.         << typeid(*tally.current()).name()
  95.         << " = " << tally->weight() << endl;
  96.     tally++;
  97.   }
  98.   os << "Total value = " << val << endl;
  99. }
  100.  
  101. main() {
  102.   // Seed the random number generator
  103.   time_t t;
  104.   srand((unsigned)time(&t));
  105.  
  106.   tstack<trash> bin; // Default to ownership
  107.   // Fill up the trash bin:
  108.   for(int i = 0; i < 30; i++)
  109.     switch(rand() % 3) {
  110.       case 0 :
  111.         bin.push(new aluminum(rand() % 100));
  112.         break;
  113.       case 1 :
  114.         bin.push(new paper(rand() % 100));
  115.         break;
  116.       case 2 :
  117.         bin.push(new glass(rand() % 100));
  118.         break;
  119.     }
  120.   // Note difference w/ chapter 14: Bins hold
  121.   // exact type of object, not base type:
  122.   tstack<glass> glassbin(0); // No ownership
  123.   tstack<paper> paperbin(0);
  124.   tstack<aluminum> ALbin(0);
  125.   tstackIterator<trash> sorter(bin);
  126.   // Sort the trash:
  127.   while(sorter) {
  128.     aluminum* ap =
  129.       dynamic_cast<aluminum*>(sorter.current());
  130.     paper* pp =
  131.       dynamic_cast<paper*>(sorter.current());
  132.     glass* gp =
  133.       dynamic_cast<glass*>(sorter.current());
  134.     if(ap) ALbin.push(ap);
  135.     if(pp) paperbin.push(pp);
  136.     if(gp) glassbin.push(gp);
  137.     sorter++;
  138.   }
  139.   SumValue(ALbin, out);
  140.   SumValue(paperbin, out);
  141.   SumValue(glassbin, out);
  142.   SumValue(bin, out);
  143. }
  144.